<?php
/*
* Author: Nguyen Hai Nam
* Module: Config_TranslateModule
* Desc	: ""
* Date	: 12/12/2011
*/
class Config_TranslateModule extends Pick_Core_Module{

	public $authentication = 1;

	function Config_TranslateModule(){
		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		
		// Process ajax for postion cof module
		if($act == "position"){
			$modules = new Pick_Core_Modules();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $modules->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();	
		}
		
	}
	public function listAction(){

		$languages = new Pick_Core_Languages();
        $languagesother = new Pick_Core_LanguagesOther();
		$actions 		= new Pick_Core_Actions();

		$actions->setInfoAction($languagesother, $this->request);

		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		$infoText = '';
		$infoClass = 'infoText';

		# Read parameters from request
		$plus 			= $this->request->element('plus','');
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchLang 	= $this->request->element('searchLang','');
		$page 			= $this->request->element('page',1);
		$orderBy 		= $this->request->element('orderBy', 'default_text');
		$orderDir 		= $this->request->element('orderDir', 'ASC');
		$ajax 			= $this->request->element('ajax', false);
		$theme 			= $this->request->element('theme', '');

		# Call ajax for theme
		if($ajax == true && $theme != ''){
			$strOption = optionLayout($theme, '');
			echo $strOption;
			exit();
		}

		# Control plus action
		if($plus){
			$actions->auto($plus);
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";
		}

        if(empty($searchLang)){
            $searchLang = $languages->getDefaultLanguage();
        }
		# Build search condition
        $condition = array();
        if($searchTerms != '') {	
            $condition['searchCond'] = " default_text LIKE '%".$searchTerms."%' OR default_text LIKE '%".$searchTerms."%'";
        }
        if($searchLang !=''){
            $condition['searchLang'] = $searchLang;
        }
		# Get all categories object and pass to template
		$listObjects = $languagesother->getObjectsFromTwoTable($page, $condition, array($orderBy => $orderDir), $items_per_pages);
        
		# Generate status combobox for Search form
		$listLanguages = $languages->creatOption($searchLang, 'code', 'name');

		# Pass some variables to template
		$this->template->assign('page',$page);
		$this->template->assign('listObjects',$listObjects);
		$this->template->assign('searchTerms',$searchTerms);
        $this->template->assign('searchLang',$searchLang);
		$this->template->assign('listLanguages',$listLanguages);
		$this->template->assign('orderBy',$orderBy);
		$this->template->assign('orderDir',$orderDir);
		$this->template->assign('infoText',$infoText);
		$this->template->assign('infoClass',$infoClass);
	}

	public function addAction(){
		$languagedefault 	= new Pick_Core_LanguagesDefault();
		$languageother 	= new Pick_Core_LanguagesOther();
		$objectInfo 	= new Pick_Core_LanguagesOtherInfo();
		
		$oId = $this->request->element('oId','');
        $languages_other = $this->request->element('languages_other','');
        $code = $this->request->element('code','');
		$plus = $this->request->element('plus','');
		$infoText = '';
		$infoClass = '';
		
		# Read parameters from request
		$arrayFieldFromMySQL = $languageother->getListFields();
		$object = $languageother->getObject($oId);

        # Read parameters from request
		$arrayFieldFromMySQL = $languageother->getListFields();
		$object = $languageother->getObject($oId);
		if(is_object($object) && $this->request->post('plus')==""){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $object->getField($fieldName);
			}#endforeach	
		}else{
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->element($fieldName,'');
			}#endforeach
		}

		# Put data into object
		$objectInfo->setArrayField($fields);
		$this->template->assign("objectInfo", $objectInfo);
		
		# Truy du lieu qua template
		foreach($arrayFieldFromMySQL as $fieldName){
				$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach
		
		# Begin process data
		if($_POST && $plus == 'saveItem') {
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			if($validatemess == '') {
				/* Test if it's exists then can't add theme more'' */
				$objLanguageOther = "";
				$arrayCheck = array(
                    'code' => $code,
                    'language_default_id' => $languages_other
                );
				$objLanguageOther = $languageother->checkDataExits($arrayCheck);
                if(is_object($objLanguageOther)){
                    $oId = $objLanguageOther->getField('id');
                }
                $ok = $languageother->updateData($objectInfo->getArrayField(),$oId);
                $infoText = $validatemess;
                if($ok){
                    $infoText = $this->l('item_save_ok');
                    $infoClass = "InfoText";
                    $oId = $oId?$oId:mysql_insert_id();			
                }
				
			}else {
				$infoText = $this->l('error').":<br />".$validatemess;
				$infoClass = "errorText";
			}
		}
		$this->template->assign("oId",$oId);
        $this->template->assign("languages_other",$languages_other);
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);
        $this->template->assign("languagedefault",$languagedefault);
	}

	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validString($this->l('Translate Text'),$request->element('translate_text'));	
		return $infoText;
	}	
}